export function formatDate(time, fmt) {
  if (!(time instanceof Date && !isNaN(time.getTime()))) {
    return ''
  }
  const date = new Date(time)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

export function formatDate2(time, fmt) {
  const date = new Date(time)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}

export function formatReqStatus(syncStatus) {
  let rs = ''
  if (syncStatus === '0') {
    rs = '领料单执行中'
  } else if (syncStatus === '1') {
    rs = '领料单关闭'
  } else if (syncStatus === '2') {
    rs = '领料单驳回'
  }
  return rs
}

export function formatSyncStatus(syncStatus) {
  let rs = ''
  if (syncStatus === '0') {
    rs = '出库申请中'
  } else if (syncStatus === '1') {
    rs = '出库同步中'
  } else if (syncStatus === '2') {
    rs = '出库同步成功'
  } else if (syncStatus === '3') {
    rs = '出库失败'
  } else if (syncStatus === '4') {
    rs = '出库驳回'
  }
  return rs
}

export function formatSyncStatusForCancel(syncStatus) {
  let rs = ''
  if (syncStatus === '0') {
    rs = '冲销申请中'
  } else if (syncStatus === '1') {
    rs = '冲销同步中'
  } else if (syncStatus === '2') {
    rs = '冲销同步成功'
  } else if (syncStatus === '3') {
    rs = '冲销失败'
  } else if (syncStatus === '4') {
    rs = '冲销驳回'
  }
  return rs
}

export function formatContractType(conType) {
  let rs = ''
  if (conType === 'Z01') {
    rs = '项目物资闭口合同'
  } else if (conType === 'Z02') {
    rs = '项目物资零星合同'
  } else if (conType === 'Z03') {
    rs = '项目物资开口合同'
  } else if (conType === 'Z04') {
    rs = '项目物资代购合同'
  } else if (conType === 'Z05') {
    rs = '项目物资委外原材料合同'
  } else if (conType === 'Z06') {
    rs = '项目服务委外加工件合同'
  } else if (conType === 'Z07') {
    rs = '项目服务分包合同'
  } else if (conType === 'Z08') {
    rs = '项目服务承包合同'
  } else if (conType === 'Z09') {
    rs = '项目通用专项合同'
  } else {
    rs = '贸易物资合同'
  }
  return rs
}

export function formatVendorType(venType) {
  let rs = ''
  if (venType === '001') {
    rs = '采购核心'
  } else if (venType === 'Z02') {
    rs = '项目物资零星合同'
  } else if (venType === 'Z03') {
    rs = '项目物资开口合同'
  } else if (venType === 'Z04') {
    rs = '项目物资代购合同'
  } else if (venType === 'Z05') {
    rs = '项目物资委外原材料合同'
  } else {
    rs = '贸易物资合同'
  }
  return rs
}

export function tableChangeRowClass(record, index) {
  console.log(record)
  if (record.parentId === '') {
    return ''
  } else if (record.instructNumber !== '') {
    return 'rowClassBlue'
  } else {
    return 'rowClassRed'
  }
}

export function formatMoney(number, decimals = 0, decPoint = '.', thousandsSep = ',') {
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  const n = !isFinite(+number) ? 0 : +number
  const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
  const sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
  const dec = (typeof decPoint === 'undefined') ? '.' : decPoint
  let s = ''
  const toFixedFix = function(n, prec) {
    const k = Math.pow(10, prec)
    return '' + Math.ceil(n * k) / k
  }
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  const re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}

export function formatDecimals(number, decimals = 2) {
  return formatMoney(number, decimals, '.', '')
}

const apiconst = {
  majorData:[
     { value: 'P01', label: 'P01/机械工程', name: '机械工程'},
     { value: 'P02', label: 'P02/电气工程', name: '电气工程' }, 
     { value: 'P03', label: 'P03/弱电工程', name: '弱电工程' },
     { value: 'P04', label: 'P04/洁净内装工程', name: '洁净内装工程' },
     { value: 'P05', label: 'P05/工艺管道工程', name: '工艺管道工程'}, 
     { value: 'P06', label: 'P06/消防工程', name: '消防工程'},
     { value: 'P07', label: 'P07/建筑内装工程', name: '建筑内装工程' },
     { value: 'P08', label: 'P08/杂项工程', name: '杂项工程' }
    ],
    packageData:[
      { value: 'P01J1', name: '空调通风工程', label: 'P01J1/空调通风工程' },
      { value: 'P01J2', name: '一般管道工程', label: 'P01J2/一般管道工程' },
      { value: 'P01J3', name: '机械保温工程', label: 'P01J3/机械保温工程' },
      { value: 'P02D1', name: '变电所工程', label: 'P02D1/变电所工程' },
      { value: 'P02D2', name: '电气工程', label: 'P02D2/电气工程' },
      { value: 'P03R1', name: '自控工程', label: 'P03R1/自控工程' },
      { value: 'P03R2', name: '安防工程', label: 'P03R2/安防工程' },
      { value: 'P03R3', name: '其他弱电工程', label: 'P03R3/其他弱电工程' },
      { value: 'P04N1', name: '系统吊顶工程', label: 'P04N1/系统吊顶工程' },
      { value: 'P04N2', name: '墙板工程', label: 'P04N2/墙板工程' },
      { value: 'P04N3', name: '高架地板工程', label: 'P04N3/高架地板工程' },
      { value: 'P04N4', name: '环氧涂装工程', label: 'P04N4/环氧涂装工程' },
      { value: 'P05G1', name: '工艺通风工程', label: 'P05G1/工艺通风工程' },
      { value: 'P05G2', name: '工艺水管道工程', label: 'P05G2/工艺水管道工程' },
      { value: 'P05G3', name: '大宗气体管道工程', label: 'P05G3/大宗气体管道工程' },
      { value: 'P05G4', name: '特气管道工程', label: 'P05G4/特气管道工程' },
      { value: 'P05G5', name: '食药洁净管道工程', label: 'P05G5/食药洁净管道工程' },
      { value: 'P05G6', name: '化学品管道工程', label: 'P05G6/化学品管道工程' },
      { value: 'P05G7', name: '工艺物料管道工程', label: 'P05G7/工艺物料管道工程' },
      { value: 'P05G8', name: '工艺保温工程', label: 'P05G8/工艺保温工程' },
      { value: 'P06X1', name: '消防管道工程', label: 'P06X1/消防管道工程' },
      { value: 'P06X2', name: '消防报警工程', label: 'P06X2/消防报警工程' },
      { value: 'P07T1', name: '土建工程', label: 'P07T1/土建工程' },
      { value: 'P07T2', name: '一般装修工程', label: 'P07T2/一般装修工程' },
      { value: 'P07T3', name: '钢结构工程', label: 'P07T3/钢结构工程' },
      { value: 'P07T4', name: '防火封堵工程', label: 'P07T4/防火封堵工程' },
      { value: 'P08Z1', name: '土建装修杂项工程', label: 'P08Z1/土建装修杂项工程' },
      { value: 'P08Z2', name: '综合杂项工程', label: 'P08Z2/综合杂项工程' }
     ]
}

export default apiconst